<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>CSS 学习笔记-选择器 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/28.ce131de6.js" as="script"><link rel="preload" href="/assets/js/10.265e4f40.js" as="script"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/15.92adafd4.js"><link rel="prefetch" href="/assets/js/16.bcee7c14.js"><link rel="prefetch" href="/assets/js/17.ee1bcf41.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/20.44781dae.js"><link rel="prefetch" href="/assets/js/21.1fe46223.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>CSS 学习笔记-选择器</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20200321.html#基本选择器" class="sidebar-link">基本选择器</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20200321.html#标签选择器" class="sidebar-link">标签选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#id-选择器" class="sidebar-link">ID 选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#类选择器" class="sidebar-link">类选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#通配符" class="sidebar-link">通配符</a></li></ul></li><li><a href="/Views/20200321.html#扩展选择器" class="sidebar-link">扩展选择器</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20200321.html#后代选择器" class="sidebar-link">后代选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#子代选择器" class="sidebar-link">子代选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#交集选择器" class="sidebar-link">交集选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#并集选择器" class="sidebar-link">并集选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#兄弟选择器" class="sidebar-link">兄弟选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#属性选择器" class="sidebar-link">属性选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#伪类-伪类选择器" class="sidebar-link">伪类(伪类选择器)</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#结构伪类选择器" class="sidebar-link">结构伪类选择器</a></li><li class="sidebar-sub-header"><a href="/Views/20200321.html#伪元素选择器" class="sidebar-link">伪元素选择器</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>CSS 学习笔记-选择器</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>CSS 学习笔记-选择器</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2020-03-21</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20200321.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      CSS
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p>CSS 选择器就是指定 CSS 要作用的标签的名称。即选择哪个容器来应用这种 CSS 样式。CSS 选择器可以分为基本选择器与扩展选择器
</p> <h2 id="基本选择器"><a href="#基本选择器" class="header-anchor">#</a> 基本选择器</h2> <h3 id="标签选择器"><a href="#标签选择器" class="header-anchor">#</a> 标签选择器</h3> <p>指定一种标签，将 CSS 应用于页面上所有的选定标签</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有的 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="id-选择器"><a href="#id-选择器" class="header-anchor">#</a> ID 选择器</h3> <p>将 CSS 应用于指定 ID 的标签，通过  <code># + id</code>  的方式指定</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">#title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于 ID 为 title 的标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="类选择器"><a href="#类选择器" class="header-anchor">#</a> 类选择器</h3> <p>将 CSS 应用于所有指定 class 的标签，通过  <code>. + class</code>  的方式指定</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">.title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 class 为 title 的标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="custom-block tip"><p class="custom-block-title">ID 选择器与类选择器</p> <p>每个标签都可以带有 id 属性与 class 属性，不同的标签 id 不能相同但可以有相同的 class，同一个标签可以带有多个 class 属性，这些属性通过空格分隔，因此类选择器使用比较灵活，也是指定 CSS 最常使用的方式</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token comment">&lt;!-- 对 p 标签使用多个 class --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>class1 class2 class3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div> <h3 id="通配符"><a href="#通配符" class="header-anchor">#</a> 通配符</h3> <p>通配符  <code>*</code>  可以匹配所有的标签，一般情况下尽量不要使用</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">*</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="扩展选择器"><a href="#扩展选择器" class="header-anchor">#</a> 扩展选择器</h2> <h3 id="后代选择器"><a href="#后代选择器" class="header-anchor">#</a> 后代选择器</h3> <p>当我们需要选中指定标签下的所有指定子标签就可以使用后代选择器。后代选择器通过将两个或多个基本选择器用空格分隔来定义。</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">div p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;div&gt; 标签下的所有 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">p .title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;p&gt; 标签下的所有 class 为 title 的标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">div p .title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于 &lt;div&gt; 标签下的所有 &lt;p&gt; 标签下的所有 class 为 title 的标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="子代选择器"><a href="#子代选择器" class="header-anchor">#</a> 子代选择器</h3> <p>当我们需要选中指定标签下的子代标签时可以使用子代选择器。与后代选择器不同，子代指的是直接后代。子代选择器通过 <code>&gt;</code>  来定义</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">div&gt;p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;div&gt; 标签下的子代 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="交集选择器"><a href="#交集选择器" class="header-anchor">#</a> 交集选择器</h3> <p>当我们需要通过多个条件选中指定标签的时候可以使用交集选择器。定义交集选择器需要将基本选择器紧密相连通常标签选择器在前</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">p.title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 class 为 title 的 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">div#title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于 id 为 title 的 &lt;div&gt; 标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">div.title.red</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于同时包含 title 与 red 两个 class 的 &lt;div&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="并集选择器"><a href="#并集选择器" class="header-anchor">#</a> 并集选择器</h3> <p>当我们需要对多种标签使用相同样式时可以使用并集选择器。并集选择器将要选择的基本选择器用 <code>,</code>  分隔</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">div,p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;div&gt; 标签和 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">p,.title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;p&gt; 标签和所有 class 为 title 的标签 */</span>
<span class="token punctuation">}</span>
<span class="token selector">div,p,#title</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;div&gt; 标签和所有 &lt;p&gt; 标签以及所有 id 为 title 的标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="兄弟选择器"><a href="#兄弟选择器" class="header-anchor">#</a> 兄弟选择器</h3> <p>当我们需要选择某个标签后面的兄弟标签时可以使用兄弟选择器。</p> <h4 id="当我们需要选中下一个相邻的兄弟标签时通过-来定义"><a href="#当我们需要选中下一个相邻的兄弟标签时通过-来定义" class="header-anchor">#</a> 当我们需要选中下一个相邻的兄弟标签时通过  <code>+</code>  来定义</h4> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">div+p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有与 &lt;div&gt; 标签相邻的下一个 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="当我们需要选中后面所有的兄弟标签时通过-来定义"><a href="#当我们需要选中后面所有的兄弟标签时通过-来定义" class="header-anchor">#</a> 当我们需要选中后面所有的兄弟标签时通过  <code>~</code>  来定义</h4> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">div~p</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有 &lt;div&gt; 标签后的兄弟 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="属性选择器"><a href="#属性选择器" class="header-anchor">#</a> 属性选择器</h3> <p>当需要通过元素的属性以及属性的值来选择元素时就可以使用属性选择器，属性选择器通过  <code>[]</code>  来表示</p> <h4 id="简单属性选择器"><a href="#简单属性选择器" class="header-anchor">#</a> 简单属性选择器</h4> <p>要选择包含指定属性的元素只需要将属性用  <code>[]</code>  包裹并放在元素后面</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">*[class]</span> <span class="token punctuation">{</span><span class="token comment">/* 此处 CSS 样式将作用于所有包含 class 属性的标签 */</span><span class="token punctuation">}</span>
<span class="token selector">a[href]</span> <span class="token punctuation">{</span><span class="token comment">/* 此处 CSS 样式将作用于所有包含 href 属性的 &lt;a&gt; 标签 */</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果要选中多个属性则只需要将属性选择器排列在一起</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a[class][href]</span> <span class="token punctuation">{</span><span class="token comment">/* 此处 CSS 样式将作用于所有同时包含 href 与 class 属性的 &lt;a&gt; 标签 */</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="根据具体属性值选择"><a href="#根据具体属性值选择" class="header-anchor">#</a> 根据具体属性值选择</h4> <p>我们可以通过  <code>=</code>  来为指定的属性配置指定的值</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a[href = &quot;https://www.baidu.com&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 href 属性且 href 值为 https://www.baidu.com 的 &lt;a&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="根据部分属性值选择"><a href="#根据部分属性值选择" class="header-anchor">#</a> 根据部分属性值选择</h4> <p>有些属性包含用空格分隔的多个值，要选择其中的某个值可以使用  <code>~=</code></p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">p[class ~= &quot;class1&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 class 属性且 class 值包含 class1 的 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面例子中的写法等价于  <code>p.class1 {}</code> 。不同的是这种写法只能用于 class 而上面的方法可以用于所有属性</p> <h4 id="根据属性字符串匹配选择"><a href="#根据属性字符串匹配选择" class="header-anchor">#</a> 根据属性字符串匹配选择<span class="badge warning" style="vertical-align:top;" data-v-43049fc3>CSS3</span></h4> <p>我们可以通过  <code>^=</code>  来指定属性的值必须以指定字符串开头</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a[href ^= &quot;https&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 href 属性且 href 的值以 https 开头的 &lt;a&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>我们可以通过  <code>$=</code>  来指定属性的值必须以指定字符串结尾</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a[href $= &quot;.com&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 href 属性且 href 的值以 .com 结尾的 &lt;a&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>我们可以通过  <code>*=</code>  来指定属性的值必须包含指定的字符串</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a[href *= &quot;baidu&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 href 属性且 href 的值包含 baidu 的 &lt;a&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="特定属性选择"><a href="#特定属性选择" class="header-anchor">#</a> 特定属性选择</h4> <p>通过  <code>|=</code>  来定义，多用于匹配语言</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">p[lang |= &quot;en&quot;]</span> <span class="token punctuation">{</span>
   <span class="token comment">/* 此处 CSS 样式将作用于所有包含 lang 属性且 lang 的值等于 en 或以 en- 开头的 &lt;p&gt; 标签 */</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Chead%3E%0A%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20%20%20p%5Blang%20%7C%3D%20%22en%22%5D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20red%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E%0A%3C%2Fhead%3E%0A%3Cbody%3E%0A%3Cp%20lang%3D%22en%22%3EHello!%3C%2Fp%3E%0A%3Cp%20lang%3D%22en-us%22%3EGreetings!%3C%2Fp%3E%0A%3Cp%20lang%3D%22fr%22%3EBonjour!%3C%2Fp%3E%0A%3Cp%20lang%3D%22cy-en%22%3EJrooana!%3C%2Fp%3E%0A%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
        <span class="token selector">p[lang |= &quot;en&quot;]</span> <span class="token punctuation">{</span>
            <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Hello!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>en-us<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Greetings!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>fr<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Bonjour!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>cy-en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Jrooana!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h3 id="伪类-伪类选择器"><a href="#伪类-伪类选择器" class="header-anchor">#</a> 伪类 (伪类选择器)</h3> <p>如果一个标签需要在不同的状态下使用不同的样式，这时就需要伪类。伪类用  <code>:</code>  表示</p> <p>下面是一些常用的伪类:</p> <ul><li><code>:link</code>  设置超链接点击之前的样式</li> <li><code>:visited</code>  设置超链接被访问之后的样式</li> <li><code>:hover</code>  设置鼠标放到标签上时的样式</li> <li><code>:active</code>  设置鼠标点击标签但不松手时的样式。</li> <li><code>:focus</code>  设置某个标签获得焦点时的样式（比如某个输入框获得焦点）</li></ul> <h4 id="超链接的四种状态"><a href="#超链接的四种状态" class="header-anchor">#</a> 超链接的四种状态</h4> <p><code>&lt;a&gt;</code>  标签有下面 4 种伪类</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a:link</span> <span class="token punctuation">{</span><span class="token comment">/* 超链接点击之前的样式 */</span><span class="token punctuation">}</span>
<span class="token selector">a:visited</span> <span class="token punctuation">{</span><span class="token comment">/* 超链接被访问之后的样式 */</span><span class="token punctuation">}</span>
<span class="token selector">a:hover</span> <span class="token punctuation">{</span><span class="token comment">/* 鼠标放到标签上(悬停)的样式 */</span><span class="token punctuation">}</span>
<span class="token selector">a:active</span> <span class="token punctuation">{</span><span class="token comment">/* 鼠标点击标签但不松手(长按)的样式 */</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="custom-block warning"><p class="custom-block-title">书写顺序</p> <p><code>a:link</code>  、 <code>a:visited</code>  、 <code>a:hover</code>  、 <code>a:active</code>  四种伪类必须按固定顺序来写，否则会失效</p></div> <div class="custom-block tip"><p class="custom-block-title">超链接美化</p> <p><code>a{}</code>  与  <code>a:link{}</code>  都可以定义超链接的样式，他们的区别是:</p> <ul><li><code>a{}</code>  定义的样式针对所有的超链接 (包含了锚点)</li> <li><code>a:link{}</code>  定义的样式针对所有写了  <code>href</code>  属性的超链接 (不包含锚点)</li></ul></div> <p>如果不同伪类的样式相同可以写在一起用逗号隔开 (使用并集选择器)</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">a:link, a:visited</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="结构伪类选择器"><a href="#结构伪类选择器" class="header-anchor">#</a> 结构伪类选择器<span class="badge warning" style="vertical-align:top;" data-v-43049fc3>CSS3</span></h3> <p>结构伪类选择器可以根据目标元素在其兄弟元素中的位置来选择。</p> <h4 id="匹配首尾元素"><a href="#匹配首尾元素" class="header-anchor">#</a> 匹配首尾元素</h4> <p><code>:first-child</code>  与  <code>:last-child</code>  可以分别匹配一组兄弟元素中的第一个与最后一个目标元素。</p> <div class="custom-block tip"><p class="custom-block-title">位置选择</p> <p>这里的位置是相对于一组兄弟元素的父元素而言，第一个元素也就是父元素的子代元素。选择器会找到所有位置符合的元素，然后查看该元素是否为指定的元素，如果类型不匹配则该位置失效</p></div> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20%2F*%20%E8%AE%BE%E7%BD%AE%E5%9F%BA%E7%A1%80%E6%A0%B7%E5%BC%8F%E5%90%8E%E9%9D%A2%E7%9A%84%E4%BE%8B%E5%AD%90%E4%B8%AD%E9%80%9A%E7%94%A8%20*%2F%0A%20%20%20%20.test%20li%20%7B%0A%20%20%20%20%20%20%20list-style%3A%20none%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test%20li%2C%20.test%20a%20%7B%0A%20%20%20%20%20%20%20display%3A%20inline-block%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test%20li%2C%20.test%20a%2C%20.test%20div%20%7B%0A%20%20%20%20%20%20%20%20min-width%3A%2040px%3B%0A%20%20%20%20%20%20%20%20height%3A%2025px%3B%0A%20%20%20%20%20%20%20%20border-color%3A%20black%3B%0A%20%20%20%20%20%20%20%20border-width%3A%201px%3B%0A%20%20%20%20%20%20%20%20border-style%3A%20solid%3B%0A%20%20%20%20%20%20%20%20text-align%3A%20center%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20%E7%BB%93%E6%9E%84%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E7%A4%BA%E4%BE%8B%20*%2F%0A%20%20%20%20.test1%20li%3Afirst-child%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20red%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test1%20li%3Alast-child%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20green%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Ch5%3E%E9%80%9A%E8%BF%87%E7%BB%93%E6%9E%84%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E5%8F%98%E6%9B%B4%E9%A6%96%E5%B0%BE%20li%20%E5%85%83%E7%B4%A0%E7%9A%84%E8%83%8C%E6%99%AF%E8%89%B2%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test1%22%3E%0A%20%20%20%20%3Cli%3E1%3C%2Fli%3E%3Cli%3E2%3C%2Fli%3E%3Cli%3E3%3C%2Fli%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Ch5%3E%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E6%98%AF%20a%20%E5%85%83%E7%B4%A0%E4%B8%8D%E6%98%AF%E7%9B%AE%E6%A0%87%20li%20%E5%85%83%E7%B4%A0%E4%BD%8D%E7%BD%AE%E6%97%A0%E6%95%88%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test1%22%3E%0A%20%20%20%20%3Ca%3E1%3C%2Fa%3E%3Cli%3E2%3C%2Fli%3E%3Cli%3E3%3C%2Fli%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token comment">/* 设置基础样式后面的例子中通用 */</span>
    <span class="token selector">.test li</span> <span class="token punctuation">{</span>
       <span class="token property">list-style</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test li, .test a</span> <span class="token punctuation">{</span>
       <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test li, .test a, .test div</span> <span class="token punctuation">{</span>
        <span class="token property">min-width</span><span class="token punctuation">:</span> 40px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span> 25px<span class="token punctuation">;</span>
        <span class="token property">border-color</span><span class="token punctuation">:</span> black<span class="token punctuation">;</span>
        <span class="token property">border-width</span><span class="token punctuation">:</span> 1px<span class="token punctuation">;</span>
        <span class="token property">border-style</span><span class="token punctuation">:</span> solid<span class="token punctuation">;</span>
        <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">/* 结构伪类选择器示例 */</span>
    <span class="token selector">.test1 li:first-child</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test1 li:last-child</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> green<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>通过结构伪类选择器变更首尾 li 元素的背景色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>第一个元素是 a 元素不是目标 li 元素位置无效<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h4 id="匹配指定位置元素"><a href="#匹配指定位置元素" class="header-anchor">#</a> 匹配指定位置元素</h4> <p>通过下面的选择器可以匹配指定位置的元素</p> <ul><li><code>:nth-child(n)</code>  : 匹配一组兄弟元素中的第 n 个元素，编号从 1 开始</li> <li><code>:nth-last-child(n)</code>  : 匹配一组兄弟元素中倒数第 n 个元素</li> <li><code>:nth-child(odd)</code>  : 匹配奇数元素</li> <li><code>:nth-child(even)</code>  : 匹配偶数元素</li></ul> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test2%20li%3Anth-child(3)%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20red%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Ch5%3E%E5%B0%86%E7%AC%AC3%E4%B8%AA%20li%20%E5%85%83%E7%B4%A0%E8%83%8C%E6%99%AF%E8%89%B2%E5%8F%98%E4%B8%BA%E7%BA%A2%E8%89%B2%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test2%22%3E%0A%20%20%20%20%3Cli%3E1%3C%2Fli%3E%3Cli%3E2%3C%2Fli%3E%3Cli%3E3%3C%2Fli%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Ch5%3E%E5%A6%82%E6%9E%9C%E7%9B%AE%E6%A0%87%E4%BD%8D%E7%BD%AE%E4%B8%8D%E6%98%AF%E6%8C%87%E5%AE%9A%E5%85%83%E7%B4%A0%E5%88%99%E8%AF%A5%E4%BD%8D%E7%BD%AE%E6%97%A0%E6%95%88%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test2%22%3E%0A%20%20%20%20%3Cli%3E1%3C%2Fli%3E%3Cli%3E2%3C%2Fli%3E%3Ca%3E3%3C%2Fa%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test2 li:nth-child(3)</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>将第3个 li 元素背景色变为红色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>如果目标位置不是指定元素则该位置无效<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <div class="custom-block tip"><p><code>:nth-child(n)</code>  除了可以填入具体的数字表示选定具体位置的元素外，还可以填入包含 n 的表达式。此时其中的 n 表示 0, 1, 2, 3 ... 等数字，通过将这些数字带入表达式计算，计算结果中大于 0 的数就是需要选中的元素的位置。</p> <ul><li><code>:nth-child(n)</code>  : 直接填入 n 表示选中所有位置的元素</li> <li><code>:nth-child(2n)</code>  : 表示所有偶数位的元素，等同于  <code>:nth-child(even)</code></li> <li><code>:nth-child(2n+1)</code>  : 表示所有奇数位的元素，等同于  <code>:nth-child(odd)</code></li> <li><code>:nth-child(-n+5)</code>  : 表示前 5 个元素 (将 0, 1, 2 ... 带入 n 计算得 5, 4, 3 ...，排除小于 0 的数可以看出是前 5 个位置)</li> <li><code>:nth-child(7n)</code>  : 表示选中 7 的倍数的位置</li></ul> <p>通过上面的规律可以根据具体的需求得出位置序列的表达式， <code>:nth-last-child(n)</code>  用法相同，不同的是得出的位置是反向的。如  <code>:nth-last-child(-n+5)</code>  表示倒数后 5 个元素</p></div> <h4 id="在指定类型中匹配指定位置的元素"><a href="#在指定类型中匹配指定位置的元素" class="header-anchor">#</a> 在指定类型中匹配指定位置的元素</h4> <p>上面的匹配指定位置元素的选择器是先找到指定位置在查看是否是指定类型，如果目标位置不是指定的类型则该位置不会被选中，也就是如果指定类型前后插入了其他类型的元素则这些其他类型的元素也会占用选择器的位置。如果我们想排除这些其他类型元素只在指定元素中按位置选择可以使用下面的方法。</p> <ul><li><code>:first-of-type</code>  : 匹配指定类型中第一个元素</li> <li><code>:last-of-type</code>  : 匹配指定类型中的最后一个元素</li> <li><code>:nth-of-type(n)</code>  : 匹配指定类型中的第 n 个元素</li> <li><code>:nth-last-of-type(n)</code>  : 匹配指定类型中的倒数第 n 个元素</li></ul> <p>这些选择器会先将非指定类型元素排除后再按位置选择</p> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test3%20li%3Anth-of-type(3)%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20red%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Ch5%3E%E5%B0%86%E7%AC%AC3%E4%B8%AA%20li%20%E5%85%83%E7%B4%A0%E8%83%8C%E6%99%AF%E8%89%B2%E5%8F%98%E4%B8%BA%E7%BA%A2%E8%89%B2%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test3%22%3E%0A%20%20%20%20%3Cli%3E1%3C%2Fli%3E%3Cli%3E2%3C%2Fli%3E%3Cli%3E3%3C%2Fli%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Ch5%3E%E6%8F%92%E5%85%A5%20a%20%E5%85%83%E7%B4%A0%E4%B8%8D%E5%BD%B1%E5%93%8D%20li%20%E5%85%83%E7%B4%A0%E7%9A%84%E4%BD%8D%E7%BD%AE%E9%80%89%E6%8B%A9%3C%2Fh5%3E%0A%3Cul%20class%3D%22test%20test3%22%3E%0A%20%20%20%20%3Cli%3E1%3C%2Fli%3E%3Cli%3E2%3C%2Fli%3E%3Ca%3E3%3C%2Fa%3E%3Cli%3E4%3C%2Fli%3E%3Cli%3E5%3C%2Fli%3E%3Cli%3E6%3C%2Fli%3E%3Cli%3E7%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test3 li:nth-of-type(3)</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>将第3个 li 元素背景色变为红色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>插入 a 元素不影响 li 元素的位置选择<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>7<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h4 id="匹配指定格式的元素"><a href="#匹配指定格式的元素" class="header-anchor">#</a> 匹配指定格式的元素</h4> <ul><li><code>:empty</code>  : 匹配没有任何内容的元素，空格也不能有</li></ul> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test4%20div%20%7B%0A%20%20%20%20%20%20%20%20float%3A%20left%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test4%20div%3Aempty%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20red%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Ch5%3E%E5%B0%86%E6%B2%A1%E6%9C%89%E5%86%85%E5%AE%B9%E7%9A%84%20div%20%E5%85%83%E7%B4%A0%E8%83%8C%E6%99%AF%E8%AE%BE%E7%BD%AE%E4%B8%BA%E7%BA%A2%E8%89%B2%3C%2Fh5%3E%0A%3Cdiv%20class%3D%22test%20test4%22%3E%0A%20%20%20%20%3Cdiv%3E2%3C%2Fdiv%3E%20%3Cdiv%3E%3C%2Fdiv%3E%20%3Cdiv%3E%20%20%3C%2Fdiv%3E%20%3Cdiv%3E%3Cspan%3E%3C%2Fspan%3E%3C%2Fdiv%3E%0A%3C%2Fdiv%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test4 div</span> <span class="token punctuation">{</span>
        <span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test4 div:empty</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>将没有内容的 div 元素背景设置为红色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test4<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <ul><li><code>:target</code>  : 匹配相关 URL 指向的元素，要配合锚点使用。</li></ul> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test5%20div%2C%20.test5%20a%20%7B%0A%20%20%20%20%20%20%20%20display%3A%20inline-block%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test5%20a%20%7B%0A%20%20%20%20%20%20%20%20min-width%3A%2050px%3B%0A%20%20%20%20%7D%0A%20%20%20%20.test5%20%23first%3Atarget%2C%20.test5%20%23second%3Atarget%2C%20.test5%20%23third%3Atarget%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20red%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Ch5%3E%E5%B0%86%20URL%20%E6%8C%87%E5%90%91%E7%9A%84%E5%85%83%E7%B4%A0%E8%83%8C%E6%99%AF%E8%AE%BE%E7%BD%AE%E4%B8%BA%E7%BA%A2%E8%89%B2%3C%2Fh5%3E%0A%3Cdiv%20class%3D%22test%20test5%22%3E%0A%20%20%20%20%3Cdiv%20id%3D%22first%22%3E1%3C%2Fdiv%3E%3Cdiv%20id%3D%22second%22%3E2%3C%2Fdiv%3E%3Cdiv%20id%3D%22third%22%3E3%3C%2Fdiv%3E%0A%3C%2Fdiv%3E%0A%3Cdiv%20class%3D%22test5%22%3E%0A%20%20%20%20%3Ca%20href%3D%22%23first%22%3E%E6%8C%87%E5%90%91%201%3C%2Fa%3E%3Ca%20href%3D%22%23second%22%3E%E6%8C%87%E5%90%91%202%3C%2Fa%3E%3Ca%20href%3D%22%23third%22%3E%E6%8C%87%E5%90%91%203%3C%2Fa%3E%0A%3C%2Fdiv%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test5 div, .test5 a</span> <span class="token punctuation">{</span>
        <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test5 a</span> <span class="token punctuation">{</span>
        <span class="token property">min-width</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.test5 #first:target, .test5 #second:target, .test5 #third:target</span> <span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h5</span><span class="token punctuation">&gt;</span></span>将 URL 指向的元素背景设置为红色<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h5</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test test5<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>first<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>second<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>third<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test5<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>#first<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>指向 1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>#second<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>指向 2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>#third<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>指向 3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h3 id="伪元素选择器"><a href="#伪元素选择器" class="header-anchor">#</a> 伪元素选择器</h3> <p>伪元素通过  <code>::</code>  来设置</p> <h4 id="在元素前后添加内容"><a href="#在元素前后添加内容" class="header-anchor">#</a> 在元素前后添加内容</h4> <p><code>::before</code>  与  <code>::after</code>  配合 content 属性使用，可以在指定元素内部前后添加指定的内容同时设置内容的样式。</p> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test6%20div%3A%3Abefore%7B%0A%20%20%20%20%20%20%20%20content%3A%20%22%E6%B7%BB%E5%8A%A0%E5%89%8D%E7%BC%80%E5%86%85%E5%AE%B9%22%3B%0A%20%20%20%20%20%20%20%20color%3A%20red%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20.test6%20div%3A%3Aafter%7B%0A%20%20%20%20%20%20%20%20content%3A%20%22%E6%B7%BB%E5%8A%A0%E5%90%8E%E7%BC%80%E5%86%85%E5%AE%B9%22%3B%0A%20%20%20%20%20%20%20%20color%3A%20green%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Cdiv%20class%3D%22test6%22%3E%0A%20%20%20%20%3Cdiv%3E%20%E5%88%9D%E5%A7%8B%E5%86%85%E5%AE%B9%20%3C%2Fdiv%3E%0A%3C%2Fdiv%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test6 div::before</span><span class="token punctuation">{</span>
        <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">&quot;添加前缀内容&quot;</span><span class="token punctuation">;</span>
        <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.test6 div::after</span><span class="token punctuation">{</span>
        <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">&quot;添加后缀内容&quot;</span><span class="token punctuation">;</span>
        <span class="token property">color</span><span class="token punctuation">:</span> green<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test6<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span> 初始内容 <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h4 id="设置元素内文本样式"><a href="#设置元素内文本样式" class="header-anchor">#</a> 设置元素内文本样式</h4> <ul><li><code>::first-letter</code>  : 设置元素内第一个字符的样式</li> <li><code>::first-line</code>  : 设置元素内第一行的样式</li> <li><code>::selection</code>  : 设置元素内被鼠标选中的区域样式</li></ul> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%3Cstyle%3E%0A%20%20%20%20.test7%20p%3A%3Afirst-letter%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2020px%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20.test7%20p%3A%3Afirst-line%7B%0A%20%20%20%20%20%20%20%20color%3A%20red%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20.test7%20p%3A%3Aselection%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20yellow%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E%0A%0A%3Cdiv%20class%3D%22test7%22%3E%0A%20%20%20%20%3Cp%3E%E6%B8%90%E8%BF%9B%E5%A2%9E%E5%BC%BA%E5%92%8C%E4%BC%98%E9%9B%85%E9%99%8D%E7%BA%A7%E8%BF%99%E4%B8%A4%E4%B8%AA%E6%A6%82%E5%BF%B5%E6%98%AF%E5%9C%A8%20CSS3%20%E5%87%BA%E7%8E%B0%E4%B9%8B%E5%90%8E%E7%81%AB%E8%B5%B7%E6%9D%A5%E7%9A%84%E3%80%82%E7%94%B1%E4%BA%8E%E4%BD%8E%E7%BA%A7%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%8D%E6%94%AF%E6%8C%81%20CSS3%EF%BC%8C%E4%BD%86%E6%98%AF%20CSS3%20%E7%89%B9%E6%95%88%E5%A4%AA%E4%BC%98%E7%A7%80%E4%B8%8D%E5%BF%8D%E6%94%BE%E5%BC%83%EF%BC%8C%E6%89%80%E4%BB%A5%E5%9C%A8%E9%AB%98%E7%BA%A7%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%AD%E4%BD%BF%E7%94%A8CSS3%EF%BC%8C%E8%80%8C%E5%9C%A8%E4%BD%8E%E7%BA%A7%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8F%AA%E4%BF%9D%E8%AF%81%E6%9C%80%E5%9F%BA%E6%9C%AC%E7%9A%84%E5%8A%9F%E8%83%BD%E3%80%82%E4%BA%8C%E8%80%85%E7%9A%84%E7%9B%AE%E7%9A%84%E9%83%BD%E6%98%AF%E5%85%B3%E6%B3%A8%E4%B8%8D%E5%90%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%8B%E7%9A%84%E4%B8%8D%E5%90%8C%E4%BD%93%E9%AA%8C%EF%BC%8C%E4%BD%86%E6%98%AF%E5%AE%83%E4%BB%AC%E4%BE%A7%E9%87%8D%E7%82%B9%E4%B8%8D%E5%90%8C%EF%BC%8C%E6%89%80%E4%BB%A5%E5%AF%BC%E8%87%B4%E4%BA%86%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B%E4%B8%8A%E7%9A%84%E4%B8%8D%E5%90%8C%3C%2Fp%3E%0A%20%20%20%20%3Cp%3E%E6%B8%90%E8%BF%9B%E5%A2%9E%E5%BC%BA%E4%B8%80%E5%BC%80%E5%A7%8B%E5%B0%B1%E9%92%88%E5%AF%B9%E4%BD%8E%E7%89%88%E6%9C%AC%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E8%A1%8C%E6%9E%84%E5%BB%BA%E9%A1%B5%E9%9D%A2%EF%BC%8C%E5%AE%8C%E6%88%90%E5%9F%BA%E6%9C%AC%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E7%84%B6%E5%90%8E%E5%86%8D%E9%92%88%E5%AF%B9%E9%AB%98%E7%BA%A7%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E8%A1%8C%E6%95%88%E6%9E%9C%E3%80%81%E4%BA%A4%E4%BA%92%E3%80%81%E8%BF%BD%E5%8A%A0%E5%8A%9F%E8%83%BD%E8%BE%BE%E5%88%B0%E6%9B%B4%E5%A5%BD%E7%9A%84%E4%BD%93%E9%AA%8C%E3%80%82%3C%2Fp%3E%0A%20%20%20%20%3Cp%3E%E4%BC%98%E9%9B%85%E9%99%8D%E7%BA%A7%E4%B8%80%E5%BC%80%E5%A7%8B%E5%B0%B1%E6%9E%84%E5%BB%BA%E7%AB%99%E7%82%B9%E7%9A%84%E5%AE%8C%E6%95%B4%E5%8A%9F%E8%83%BD%EF%BC%8C%E7%84%B6%E5%90%8E%E9%92%88%E5%AF%B9%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B5%8B%E8%AF%95%E5%92%8C%E4%BF%AE%E5%A4%8D%E3%80%82%E6%AF%94%E5%A6%82%E4%B8%80%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8%20CSS3%20%E7%9A%84%E7%89%B9%E6%80%A7%E6%9E%84%E5%BB%BA%E4%BA%86%E4%B8%80%E4%B8%AA%E5%BA%94%E7%94%A8%EF%BC%8C%E7%84%B6%E5%90%8E%E9%80%90%E6%AD%A5%E9%92%88%E5%AF%B9%E5%90%84%E5%A4%A7%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E8%A1%8C%20hack%20%E4%BD%BF%E5%85%B6%E5%8F%AF%E4%BB%A5%E5%9C%A8%E4%BD%8E%E7%89%88%E6%9C%AC%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%8A%E6%AD%A3%E5%B8%B8%E6%B5%8F%E8%A7%88%3C%2Fp%3E%0A%3C%2Fdiv%3E%0A%0A%3C%2Fhtml%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.test7 p::first-letter</span><span class="token punctuation">{</span>
        <span class="token property">font-size</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.test7 p::first-line</span><span class="token punctuation">{</span>
        <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.test7 p::selection</span><span class="token punctuation">{</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>test7<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>渐进增强和优雅降级这两个概念是在 CSS3 出现之后火起来的。由于低级浏览器不支持 CSS3，但是 CSS3 特效太优秀不忍放弃，所以在高级浏览器中使用CSS3，而在低级浏览器只保证最基本的功能。二者的目的都是关注不同浏览器下的不同体验，但是它们侧重点不同，所以导致了工作流程上的不同<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>渐进增强一开始就针对低版本浏览器进行构建页面，完成基本的功能，然后再针对高级浏览器进行效果、交互、追加功能达到更好的体验。<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>优雅降级一开始就构建站点的完整功能，然后针对浏览器测试和修复。比如一开始使用 CSS3 的特性构建了一个应用，然后逐步针对各大浏览器进行 hack 使其可以在低版本浏览器上正常浏览<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/28.ce131de6.js" defer></script><script src="/assets/js/10.265e4f40.js" defer></script>
  </body>
</html>
